package scau.zhong.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import scau.zhong.common.page.PageResult;
import scau.zhong.pojo.Person;
import scau.zhong.service.impl.PersonService;

import java.util.List;

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @GetMapping("/all")
    public ResponseEntity<List<Person>> findAll(){
        List<Person> personList = personService.getAll();
        return ResponseEntity.ok(personList);
    }

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallBack")
    public ResponseEntity<Person> findById(@PathVariable(name = "id") String id){
        Person person = personService.findById(id);
        if(person == null){
            throw new RuntimeException("not found");
        }
        return ResponseEntity.ok(person);
    }

    //key: this.search, // 搜索条件
    // page: this.pagination.page,// 当前页
    // rows: this.pagination.rowsPerPage,// 每页大小
    // sortBy: this.pagination.sortBy,// 排序字段
    // desc: this.pagination.descending// 是否降序
    @GetMapping("/page")
    public ResponseEntity<PageResult<Person>> queryByPage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows
    ){
        PageResult<Person>result = this.personService.queryByPage(key,page,rows);
        if(CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/insert")
    public ResponseEntity<Void> insert(@RequestBody Person person){
        if(person == null){
            return ResponseEntity.badRequest().build();
        }
        personService.insert(person);
        return ResponseEntity.ok().build();
    }


    @PutMapping("/update")
    public ResponseEntity<Person> update(@RequestBody Person person){
        if(person == null){
            return ResponseEntity.badRequest().build();
        }
        personService.update(person);
        return ResponseEntity.ok(person);
    }

    public ResponseEntity<Person> findByIdFallBack(String id){
        Person person = new Person();
        person.setName("未知用户");
        person.setId(id);
        return ResponseEntity.ok(person);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable(value = "id") String id){
        if(StringUtils.isBlank(id)){
            return ResponseEntity.badRequest().build();
        }
        int flag = personService.deleteById(id);
        if(flag != 1){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok().build();
    }


}
